package com.lbh.cloudgallery.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbh.cloudgallery.entity.CommentEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.common.utils.PageUtils;
import com.xxx.common.utils.Query;

import com.lbh.cloudgallery.dao.AuthorDao;
import com.lbh.cloudgallery.entity.AuthorEntity;
import com.lbh.cloudgallery.service.AuthorService;


@Service("authorService")
public class AuthorServiceImpl extends ServiceImpl<AuthorDao, AuthorEntity> implements AuthorService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<AuthorEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key).or().like("name",key);
        }
        IPage<AuthorEntity> page = this.page(
                new Query<AuthorEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, Object> getAuthorList(Page<AuthorEntity> pageAuthor) {
        QueryWrapper<AuthorEntity> wrapper=new QueryWrapper<>();
        wrapper.orderByAsc("name");
        //把分页数据封装到pageAuthor对象
        baseMapper.selectPage(pageAuthor,wrapper);

        List<AuthorEntity> records=pageAuthor.getRecords();
        long current=pageAuthor.getCurrent();
        long pages=pageAuthor.getPages();
        long size=pageAuthor.getSize();
        long total=pageAuthor.getTotal();

        boolean hasNext=pageAuthor.hasNext();
        boolean hasPrevious=pageAuthor.hasPrevious();

        //将分页数据获取出来，放到mp集合
        Map<String,Object> map=new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

}